package com.lilin.gps.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

import android.util.Log;

/**
 * @author liyc
 * @time 2012-1-9 上午11:19:48
 * @annotation 日志帮助类
 */
public class LogUtils {
	
	public static String LogPath = "/sdcard/gpstest/log";
	
	public static void LogI(String msg){
		Log.i("andli", msg);
	}

	public static void LogE(String msg){
		Log.e("andli", msg);
	}

	/**
	 * 将日志写入到SD卡
	 */
	public static void Log2SDInfo(String log){
		try{
			LogI(log);
			String logName = DateUtils.convertDate(new Date(), "yyyy-MM-dd")+".log";
			File file = new File(LogPath);
			if(!file.exists()){
				file.mkdirs();
			}
			PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(LogPath+"/"+logName, true)));
			pw.write("[andli]");
			String dataTag = "["+DateUtils.convertDate(new Date(), "HH:mm:ss")+"]";
			pw.write(dataTag);
			pw.write(log);
			pw.write("\r\n");
			pw.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	//记录GPS数据到SDCard
	public static void Log2SDInfo(String log,String path){
		try{
			//LogI("SDCard写入成功!");
			String logName = DateUtils.convertDate(new Date(), "yyyy-MM-dd")+".log";
			File file = new File(path);
			if(!file.exists()){
				file.mkdirs();
			}
			PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(path+"/"+logName, true)));
			pw.write("[GPS] ");
			String dataTag = " ["+DateUtils.convertDate(new Date(), "HH:mm:ss")+"] ";
			pw.write(dataTag);
			pw.write(log);
			pw.write("\r\n");
			pw.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 将错误日志写入到SD卡
	 */
	public static void Log2SDErr(Exception e){
		try{
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
			String msg = sw.toString();
			LogE(msg);
			String logName = DateUtils.convertDate(new Date(), "yyyy-MM-dd")+".log";
			File file = new File(LogPath);
			if(!file.exists()){
				file.mkdirs();
			}
			PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(LogPath+"/"+logName, true)));
			pw.write("[andli-ERROR]");
			String dataTag = "["+DateUtils.convertDate(new Date(), "HH:mm:ss")+"]";
			pw.write(dataTag);
			pw.write(msg);
			pw.write("\r\n");
			pw.close();
		}catch(Exception e2){
			e2.printStackTrace();
		}
	}
}
